<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

global $shoutbox;
$shoutbox = new Shoutbox();
$shoutbox->add_filter( 'print_user', 'print_user_filter' );
$shoutbox->add_filter( 'print_time', 'print_time_filter' );
$shoutbox->add_filter( 'print_public_title', 'print_public_title_filter' );
$shoutbox->add_filter( 'print_group_title', 'print_group_title_filter' );
$shoutbox->add_action( 'before_messages', 'before_messages_action' );

$page = 1;
if( isset( $_GET[ 'page' ] ) && is_numeric( $_GET[ 'page' ] ) && $_GET[ 'page' ] > 0 ) {
	$page = $_GET[ 'page' ];
}

$how_many = 10;
if( isset( $_GET[ 'amount' ] ) && is_numeric( $_GET[ 'amount' ] ) && ( $_GET[ 'amount' ] == 20 || $_GET[ 'amount' ] == 50 ) ) {
	$how_many = $_GET[ 'amount' ];
}

$force_update = false;
if( isset( $_GET[ 'update' ] ) && $_GET[ 'update' ] == 'force' ) {
	$force_update = true;
}

$channel_id = -1;
$approved = false;
$initialize = false;
$group_id = -1;
if( isset( $_GET[ 'initialize' ] ) ) {
	$query = "SELECT last_channel " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$_GET[ 'channel_id' ] = $row[ 'last_channel' ];
	
	$query = "SELECT count(*) " .
			"FROM chat_channels " .
			"WHERE channel_id = " . $row[ 'last_channel' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( $row[ 'count(*)' ] == 0 ) {
		$_GET[ 'channel_id' ] = 1;
	}
	$initialize = true;
}

if( isset( $_GET[ 'channel_id' ] ) && is_numeric( $_GET[ 'channel_id' ] ) && $_GET[ 'channel_id' ] > 0 ) {
	$query = "SELECT group_id " .
			"FROM chat_channels " .
			"WHERE channel_id = " . $_GET[ 'channel_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'group_id' ] ) && $row[ 'group_id' ] != -1 ) {
		$group_id = $row[ 'group_id' ];
		
		$query = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'faction_id' ] ) && $row[ 'faction_id' ] == $group_id ) {
			$channel_id = $_GET[ 'channel_id' ];
			$approved = true;
		}
		else {
			// No access to this one, so default to the public channel
			$channel_id = 1;
			$approved = true;
		}
	}
	elseif( isset( $row[ 'group_id' ] ) && $row[ 'group_id' ] == -1 ) {
		$channel_id = $_GET[ 'channel_id' ];
		$approved = true;
	}
	elseif( !isset( $row[ 'group_id' ] ) ) {
		// No valid channel id, default to public channel
		$channel_id = 1;
		$approved = true;
	}
}

if( $approved ) {
	$highlight = Array();
	$query = "SELECT name " .
			"FROM ruler " .
			"WHERE user_id = " . $_SESSION[ 'user_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
   $highlight[ $row[ 'name' ] ] = "<strong class=\"green\">" . $row[ 'name' ] . "</strong>";
	
	$query = "SELECT name " .
			"FROM faction_member " .
			"JOIN faction ON faction_member.faction_id = faction.faction_id " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'name' ] ) ) {
   		$highlight[ $row[ 'name' ] ] = "<strong class=\"blue\">" . $row[ 'name' ] . "</strong>";
	}
	
	$query = "UPDATE ruler " .
			"SET last_channel = $channel_id " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$query = "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$user_groups = Array();
	if( isset( $row[ 'faction_id' ] ) ) {
		$user_groups[] = $row[ 'faction_id' ];
	}
	
	$shoutbox->print_shoutbox_content( $_SESSION[ 'ruler_id' ], $channel_id, $user_groups, $how_many, $page, ( $initialize || $force_update ), $highlight );
}
else {
	transprint( 'You don\'t have access to this channel.' );
}

function print_user_filter( $user_id ) {
	$query = "SELECT name, ruler_id " .
			"FROM ruler " .
			"WHERE ruler_id = " . $user_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'name' ] ) ) {
		$name = $row[ 'name' ];
		$link = true;
		// Check if this is a friendly or not
		$color = "red";
		if( $_SESSION[ 'ruler_id' ] == $row[ 'ruler_id' ] ) {
			$color = "green";
		}
		else {
			$query = "SELECT faction_id " .
					"FROM faction_member " .
					"WHERE ruler_id = $user_id AND invited = 0 AND requested = 0";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'faction_id' ] ) ) {
				$message_faction_id = $row[ 'faction_id' ];
				$query = "SELECT faction_id " .
						"FROM faction_member " .
						"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				if( isset( $row[ 'faction_id' ] ) && $row[ 'faction_id' ] == $message_faction_id ) {
					$color = "blue";
				}
			}
		}
	}
	
	return "By <a href=\"../overview/ruler.php?name=$name\"><strong class=\"$color\">" . $name . "</strong></a>: ";	
}

function print_time_filter( $timestamp ) {
	$time = time();
	$date = $time - $timestamp;
	$days = floor( $date / 86400 );
	$hours = floor( $date / 3600 % 24 );
	$minutes = floor( $date / 60 % 60 );
	$seconds = $date % 60;
	if( $days == 1 ) {
		$date = '1 ' . translate( 'day' ) . ', ';
	}
	elseif( $days > 1 ) {
		$date = number_format( $days ) . ' ' . translate( 'days' ) . ', ';
	}
	else {
		$date = '';
	}
	if( $hours == 1 ) {
		$date .= '1 ' . translate( 'hour' ) . ', ';
	}
	else {
		$date .= number_format( $hours ) . ' ' . translate( 'hours' ) . ', ';
	}
	if( $minutes == 1 ) {
		$date .= '1 ' . translate( 'minute' ) . ' ' . translate( 'and' ) . ' ';
	}
	else {
		$date .= number_format( $minutes ) . ' ' . translate( 'minutes' ) . ' ' . translate( 'and' ) . ' ';
	}
	if( $seconds == 1 ) {
		$date .= '1 ' . translate( 'second' );
	}
	else {
		$date .= number_format( $seconds ) . ' ' . translate( 'seconds' );
	}
	
	return $date . ' ' . translate( 'ago' );
}

function before_messages_action( $messages ) {
	if( count( $messages ) > 0 ) {
		$time = time();
		
		$channel_id = $messages[0]->channel_id;
		$query = "SELECT group_id " .
				"FROM chat_channels " .
				"WHERE channel_id = $channel_id";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'group_id' ] ) ) {
			$group_id = $row[ 'group_id' ];
		}
		else {
			$group_id = -1;
		}
		
		if( $group_id != -1 ) {
			$online_time = $time - 1800;
			$query = "SELECT DISTINCT ruler.name " .
					"FROM ruler " .
					"JOIN faction_member ON faction_member.ruler_id = ruler.ruler_id " .
					"JOIN chat_channels ON faction_member.faction_id = chat_channels.group_id " .
					"LEFT JOIN chat_activity ON chat_activity.channel_id = chat_channels.channel_id AND chat_activity.user_id = ruler.ruler_id " .
					"WHERE faction_id = '" . $group_id . "' AND requested = 0 AND invited = 0 AND chat_activity.timestamp > $online_time " .
					"ORDER BY chat_activity.timestamp DESC";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$num = mysql_numrows( $result );
			$i = 0;
			transprint( 'Active' );
			print( ': ' ); 
			while ( $i < $num ) {
				if( $i > 0 ) {
					print( ", " );
				}
				print( "<a href=\"../overview/ruler.php?name=" . mysql_result( $result, $i, "name" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>\r\n" );
				$i ++;
			}
?>
		<br />
		<br />
<?php
		}
	}
}

function print_public_title_filter() {
	return translate( 'Public' );
}

function print_group_title_filter() {
	return translate( 'Faction' );
}

?>
